package neuronvisualizer;


import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class AnalysisBar extends JFrame{
	public Driver parent;
	
	private JLabel currentNeuron;
	private JLabel position;
	private JLabel coords;
	private JLabel[] incomingConnections;
	private JLabel[] outgoingConnections;
	
	private JLabel currentConnection;
	private JLabel head;
	private JLabel headLoc;
	private JLabel tail;
	private JLabel tailLoc;
	
	
	private JButton empty;
	private JButton clearConnection;
	
	private final Rectangle FRAMESIZE = new Rectangle(1000,20,200,500);
		
		
	public AnalysisBar(Driver p){
		super("Data Analysis");
		parent = p;
		try {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Nimbus".equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
		} catch (Exception e) {
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (Exception E){
				E.printStackTrace();
			}
		}
		
		incomingConnections = new JLabel[5];
		outgoingConnections = new JLabel[5];
		
		setupGUI();
        
	}
		
	public void setupGUI(){
		this.setBounds(FRAMESIZE);
		this.setResizable(false);
		this.setLayout(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		
		currentNeuron = new JLabel("No neuron selected...");
			currentNeuron.setBounds(10,10,190,20);
		
		position = new JLabel("Layer: NA, Column: 0");
			position.setBounds(50,30,190,30);
		
		coords = new JLabel("x,y,z: (0,0,0)");
			coords.setBounds(50,50,190,30);
			
		incomingConnections[0] = new JLabel("Incoming Connections: 0");
			incomingConnections[0].setBounds(10,70,190,30);
		
		String[] types = {"RS","LTS","FS","IB"};
		for(int i=1;i<5;i++){
			incomingConnections[i] = new JLabel("From "+types[i-1]+" 0 (0%)");
				incomingConnections[i].setBounds(50,70+(i*20),190,30);
		}
		
		outgoingConnections[0] = new JLabel("Outgoing Connections: 0");
			outgoingConnections[0].setBounds(10,170,190,30);

		for(int i=1;i<5;i++){
			outgoingConnections[i] = new JLabel("To "+types[i-1]+" 0 (0%)");
				outgoingConnections[i].setBounds(50,170+(i*20),190,30);
		}
		
		empty = new JButton("Clear Neuron");
			empty.setBounds(10,280,150,25);
			empty.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						parent.clearNeuron();
					}
				}
			);
			empty.setEnabled(false);
		
		
		currentConnection = new JLabel("No connection selected...");
			currentConnection.setBounds(10,340,150,25);
		
		head = new JLabel("Pre-Synap: NA");
			head.setBounds(40,360,150,25);
		headLoc = new JLabel("");
			headLoc.setBounds(40,380,150,25);
		tail = new JLabel("Post-Synap: NA");
			tail.setBounds(40,400,150,25);
		tailLoc = new JLabel("");
			tailLoc.setBounds(40,420,150,25);
			
		clearConnection = new JButton("Clear Connection");
			clearConnection.setBounds(10,440,150,25);
			clearConnection.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						parent.clearConnect();
					}
				}
			);
			clearConnection.setEnabled(false);
		
		this.add(currentNeuron);
		this.add(position);
		this.add(coords);
		for(int i=0;i<incomingConnections.length;i++){
			this.add(incomingConnections[i]);
			this.add(outgoingConnections[i]);
		}
		this.add(empty);
		
		this.add(currentConnection);
		this.add(head);
		this.add(headLoc);
		this.add(tail);
		this.add(tailLoc);
		this.add(clearConnection);
		
		this.setVisible(true);
	}
	public void setSection(Section s){
		
	}
	public void clearConnect(){
		currentConnection.setText("No connection selected...");
		head.setText("PreSynap: NA");
		headLoc.setText("");
		tail.setText("PostSynap: NA");
		tailLoc.setText("");
		clearConnection.setEnabled(false);
	}
	public void setConnect(Connection c){
		double cc = c.getCharge();
		currentConnection.setText("Synaptic Weight: "+cc);
		Neuron h = parent.getNeuron(c.getHead());
		Section hSect = parent.getSectionOfNeuron(h);
			int hCol=hSect.getCol();
			int hLay=hSect.getLay();
		Neuron t = parent.getNeuron(c.getTail());
		Section tSect = parent.getSectionOfNeuron(t);
			int tCol=tSect.getCol();
			int tLay=tSect.getLay();
		head.setText("PreSynap: "+h.getType()+"<"+h.getID()+">");
		headLoc.setText("at col,lay: ("+parent.layerTitles[hCol]+","+hLay+")");
		tail.setText("PostSynap: "+t.getType()+"<"+t.getID()+">");
		tailLoc.setText("at col,lay: ("+parent.layerTitles[tCol]+","+tLay+")");
		clearConnection.setEnabled(true);
	}
	
	public void clearNeuron(){

		String[] types = {"RS","LTS","FS","IB"};
		
		currentNeuron.setText("No neuron selected...");
		position.setText("Layer: NA, Column: 0");
		coords.setText("x,y,z: (0,0,0)");
		incomingConnections[0].setText("Incoming Connections: 0");
		outgoingConnections[0].setText("Outgoing Connections: 0");
		for(int i=1;i<incomingConnections.length;i++){
			incomingConnections[i].setText("From "+types[i-1]+": 0 (0%)");
			outgoingConnections[i].setText("To "+types[i-1]+": 0 (0%)");
		}
		empty.setEnabled(false);
		clearConnection.setEnabled(true);
	}
	
	public void setNeuron(Neuron n,ArrayList<Connection> incoming){
		currentNeuron.setText(""+n.getType()+" neuron <"+n.getID()+">");
		Section tSect = parent.getSectionOfNeuron(n);
		position.setText("Layer: "+parent.layerTitles[tSect.getLay()]+", Column: "+tSect.getCol());
		coords.setText("x,y,z: ("+n.getX()+","+n.getY()+","+n.getZ()+")");
		
		incomingConnections[0].setText("Incoming Connections: "+incoming.size());
		outgoingConnections[0].setText("Ougoing Connections: "+n.getConnections().length);
		

		String[] types = {"RS","LTS","FS","IB"};
		int[] tally = {0,0,0,0};
		int[] tallyOut = {0,0,0,0};
		for(Connection c:incoming){
			String hType = ""+parent.getNeuron(c.getHead()).getType();
			if(hType.equals("RS"))
				tally[0]++;
			else if(hType.equals("LTS"))
				tally[1]++;
			else if(hType.equals("FS"))
				tally[2]++;
			else if(hType.equals("IB"))
				tally[3]++;
		}
		for(Connection c:n.getConnections()){
			String hType =""+parent.getNeuron(c.getTail()).getType();
			if(hType.equals("RS"))
				tallyOut[0]++;
			else if(hType.equals("LTS"))
				tallyOut[1]++;
			else if(hType.equals("FS"))
				tallyOut[2]++;
			else if(hType.equals("IB"))
				tallyOut[3]++;
			
		}
		for(int i=1;i<5;i++){
			double inP = Math.floor(((double)tally[i-1])/((double)incoming.size())*100.0);
			double outP = Math.floor(((double)tallyOut[i-1])/((double)n.getConnections().length)*100.0);
			incomingConnections[i].setText("From "+types[i-1]+": "+tally[i-1]+" ("+inP+"%)");
			outgoingConnections[i].setText("To "+types[i-1]+": "+tallyOut[i-1]+" ("+outP+"%)");
		}
		empty.setEnabled(true);
	}

}
